package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.project.*;
import com.example.supplychainbackend.controller.dto.project.analysis.ProjectAnalysisDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.ProjectService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/project")
@AllArgsConstructor
public class ProjectController {
    private final ProjectService projectService;
    //项目计划
    //查询全部项目计划
    @GetMapping ("/plan/list")
    public DataResponse<List<ProjectPlanDto>> listProjectPlans(@RequestHeader("Authorization") String authHeader){
        List<ProjectPlanDto> res = projectService.listProjectPlans();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = projectService.listProjectPlansByEid(eid);
        }
        return DataResponse.success(res);
    }

    //查询项目计划
    @GetMapping ("/plan/{id}")
    public DataResponse<ProjectPlanDto> getProjectPlan(@PathVariable String id){
        ProjectPlanDto res = projectService.getProjectPlan(id);
        return DataResponse.success(res);
    }

    @PostMapping("/plan/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainPlan(@PathVariable String id) {
        UploadInfo txInfo = projectService.uploadToChainPlan(id);
        return DataResponse.success(txInfo);
    }

    //新增项目计划
    @PostMapping ("/plan")
    public DataResponse<String> createProjectPlan(@RequestHeader("Authorization") String authHeader,
                                                  @RequestBody Map<String, String> requestMap) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String salesOrderNo = requestMap.get("salesOrderNo");
        String id = projectService.createProjectPlan(salesOrderNo, userSigDto);//返回项目计划编号
        return DataResponse.success(id);
    }

    //编辑项目计划
    @PutMapping  ("/plan/{id}")
    public DataResponse<ProjectPlanDto> updateProjectPlan(@RequestHeader("Authorization") String authHeader,
                                                          @PathVariable String id, @RequestBody ProjectPlanDto form){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());ProjectPlanDto res = projectService.updateProjectPlan(id, form);
        return DataResponse.success(res);
    }

    //未使用
    //删除项目计划
//    @DeleteMapping ("/plan/{id}")
//    public DataResponse<Long> deleteProjectPlan(@PathVariable Long id){
//        projectService.deleteProjectPlan(id);
//        return DataResponse.success(id);
//    }

    //项目进度查询
    @GetMapping ("/progress/list")
    public DataResponse<List<ProjectProgressDto>> listProjectProgress(){
        List<ProjectProgressDto> res = projectService.listProjectProgress();
        return DataResponse.success(res);
    }

    //未使用
//    @GetMapping ("/progress/{id}")
//    public DataResponse<ProjectProgressDto> getProjectProgress(@PathVariable Long id){
//        ProjectProgressDto res = projectService.getProjectProgress(id);
//        return DataResponse.success(res);
//    }

    //未使用
//    @PostMapping ("/progress")
//    public DataResponse<Long> createProjectProgress(@RequestBody ProjectProgressDto form){
//        Long id = projectService.createProjectProgress(form);
//        return DataResponse.success(id);
//    }

    //未使用
//    @PutMapping ("/progress/{id}")
//    public DataResponse<ProjectProgressDto> updateProjectProgress(@PathVariable Long id, @RequestBody ProjectProgressDto form){
//        ProjectProgressDto res = projectService.updateProjectProgress(id, form);
//        return DataResponse.success(res);
//    }

    //未使用
//    @DeleteMapping ("/progress/{id}")
//    public DataResponse<Long> deleteProjectProgress(@PathVariable Long id){
//        projectService.deleteProjectProgress(id);
//        return DataResponse.success(id);
//    }

    //项目文档增删改查

    //项目文档全部查询
    @GetMapping("/docs/list")
    public DataResponse<List<ProjectDocsDto>> listProjectDocs(@RequestHeader("Authorization") String authHeader){
        List<ProjectDocsDto> res = projectService.listProjectDocs();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = projectService.listProjectDocsByEid(eid);
        }
        return DataResponse.success(res);
    }

    @PostMapping("/docs/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainDocs(@PathVariable String id) {
        UploadInfo txInfo = projectService.uploadToChainDocs(id);
        return DataResponse.success(txInfo);
    }

    //项目文档查询
    @GetMapping("/docs/{id}")
    public DataResponse<ProjectDocsDto> getProjectDocs(@PathVariable String id){
        ProjectDocsDto res = projectService.getProjectDocs(id);
        return DataResponse.success(res);
    }

    //项目文档新增 未使用
//    @PostMapping("/docs")
//    public DataResponse<String> createProjectDocs(@RequestBody ProjectDocsDto form){
//        String id = projectService.createProjectDocs(form);
//        return DataResponse.success(id);
//    }

    //项目文档修改  未使用
//    @PutMapping("/docs/{id}")
//    public DataResponse<ProjectDocsDto> updateProjectDocs(@PathVariable String id, @RequestBody ProjectDocsDto form){
//        ProjectDocsDto res = projectService.updateProjectDocs(id, form);
//        return DataResponse.success(res);
//    }

    //未使用
    //项目文档删除
//    @DeleteMapping("/docs/{id}")
//    public DataResponse<String> deleteProjectDocs(@PathVariable String id){
//        projectService.deleteProjectDocs(id);
//        return DataResponse.success(id);
//    }

    //项目文档新建文件夹
    @PostMapping("/docs/newfolder")
    public DataResponse<String> createProjectDocsFolder(@RequestBody ProjectFolderDto form){
        String folderId = projectService.createProjectDocsFolder(form);
        return DataResponse.success(folderId);
    }


    //项目文档文件夹上传文件
    //已知项目文档-项目文件夹-项目文件夹文件    如果只改项目文件夹中的文件，项目文档中对应的数据是否会同时改变呢？
    @PostMapping("/docs/file/upload")
    public DataResponse<String> uploadProjectDocsFile(@RequestParam("file") MultipartFile file, @RequestParam("folderId") String folderId) throws IOException {
        String fileURL = projectService.uploadProjectDocsFile(file, folderId);
        return DataResponse.success(fileURL);
    }
    //项目文档文件查询  查询项目文档中指定文件夹下的所有文件
    @GetMapping("/docs/folder/list")
    public DataResponse<List<ProjectFolderFileDto>> getProjectDocsFile(@RequestParam String projectId, @RequestParam String folderId){
        List<ProjectFolderFileDto> res = projectService.getProjectDocsFile(projectId, folderId);
        return DataResponse.success(res);
    }


    //项目文档文件废弃
    @PutMapping("/docs/file/aborted")
    public DataResponse<String> AbortProjectDocsFolderFile(@RequestBody AbortDocReqDto dto){
        projectService.abortProjectDocsFolderFile(dto.getFileId());
        return DataResponse.success(dto.getFileId());
    }



    //项目任务看板（项目任务查询）
    @GetMapping("/kanban")
    public DataResponse<List<ProjectNodeDto>> getProjectKanban(){
        List<ProjectNodeDto> res = projectService.getProjectNodes();
        return DataResponse.success(res);
    }

    @GetMapping("/analysis")
    public DataResponse<ProjectAnalysisDto> analysis(){
        return DataResponse.success(projectService.projectAnalysis());
    }


    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> salesErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }


}
